package com.module.ble.ui.colorSelection

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.common.app.R
import com.common.app.core.RouterHub
import com.common.app.data.bean.IntelligentTouchOptionModel
import com.common.app.data.bean.KeyBundle
import com.common.app.data.bean.KeyEvents
import com.common.app.data.bean.KeyPre
import com.common.app.helper.RouterHelper
import com.common.app.utls.ColorUtils
import com.common.base.app.extras.setOnClickListener
import com.common.base.app.extras.yes
import com.common.base.utils.AppUtils.getString
import com.common.base.utils.SpUtilsManagement
import com.common.base.utils.ViewUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.module.ble.databinding.BleDialogLightColorSelectLayoutBinding
import com.module.ble.utils.LightCustomizeColorConfig
import com.module.ble.utils.LightingEffectConfig

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/09/30
 *
 * 描述：颜色选择弹框
 *
 * 修订历史：
 *
 */
class SelectLightColorDialog(context: Context) :
    Dialog(context), View.OnClickListener {

    private val mContext: Context

    private var binding: BleDialogLightColorSelectLayoutBinding? = null

    private val selectLightColorAdapter by lazy { SelectLightColorAdapter() }

    private var busType = LightingEffectConfig.TYPE_LIGHTING_TIMING

    val mList = mutableListOf<IntelligentTouchOptionModel>()

    var lightColorDialogListener :OnSelectLightColorDialogListener? = null

    init {
        setDialogTheme()
        setCanceledOnTouchOutside(true)
        mContext = context
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // dialog 初始化的时候调用(PS:保证Dialog弹出后状态栏文字变色)
        window?.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        binding = BleDialogLightColorSelectLayoutBinding.inflate(LayoutInflater.from(context))
        binding?.root?.let { setContentView(it) }

        binding?.hrv?.adapter = selectLightColorAdapter

        selectLightColorAdapter.setOnItemChildClickListener(object : OnItemChildClickListener {
            override fun onItemChildClick(
                adapter: BaseQuickAdapter<*, *>,
                view: View,
                position: Int
            ) {
                val tempPosition = selectLightColorAdapter.data[position].id

                mList.forEach {
                    it.isSelect = it.id==tempPosition
                }

                selectLightColorAdapter.notifyDataSetChanged()
            }

        })

        binding?.btnConfirm?.setOnClickListener (onClick = this)
        binding?.tvCus?.setOnClickListener (onClick = this)

        initData()
    }

    override fun show() {
        super.show()
        //设置弹框的高为屏幕的一半宽是屏幕的宽
        val mDialogLp = this.window?.attributes

        //设置宽度
        mDialogLp?.width = ViewGroup.LayoutParams.MATCH_PARENT
//        mDialogLp?.height = (ViewUtils.getScreenHeight() * 0.4).toInt()
        mDialogLp?.height = ViewGroup.LayoutParams.WRAP_CONTENT

        mDialogLp?.gravity = Gravity.BOTTOM //设置dialog 在布局中的位置
        this.window?.attributes = mDialogLp
        window?.setWindowAnimations(R.style.bottomShowStyle) //添加动画:
    }

    fun show(type:Int) {
        show()

        busType = type
        val tempName = SpUtilsManagement.getString(when(busType){
            LightingEffectConfig.TYPE_LIGHTING_GESTURE->KeyPre.KEY_LIGHTING_GESTURE_COLOR_VALUE
            LightingEffectConfig.TYPE_LIGHTING_SPORTS->KeyPre.KEY_LIGHTING_SPORTS_COLOR_VALUE
            else -> KeyPre.KEY_LIGHTING_TIMING_COLOR_VALUE
        }, ColorUtils.defColor)

        mList.forEach {
            it.isSelect = it.name==tempName
        }

        selectLightColorAdapter.notifyDataSetChanged()
    }


    override fun onClick(v: View) {
        when (v) {
            binding?.btnConfirm ->{
                // 找到选中项的索引
                val selectedIndex = mList.indexOfFirst { it.isSelect }

                if (selectedIndex != -1) {
//                    val selectedItem = mList[selectedIndex]
//                    SpUtilsManagement.putInt(when(busType){
//                        LightingEffectConfig.TYPE_LIGHTING_GESTURE->KeyPre.KEY_LIGHTING_GESTURE_COLOR_VALUE
//                        LightingEffectConfig.TYPE_LIGHTING_SPORTS->KeyPre.KEY_LIGHTING_SPORTS_COLOR_VALUE
//                        else -> KeyPre.KEY_LIGHTING_TIMING_COLOR_VALUE
//                    }, selectedItem.id)
                    lightColorDialogListener?.onSelectLightColorClick(mList[selectedIndex])
                }
            }
            binding?.tvCus ->{
                RouterHelper.navigationPostcard(RouterHub.ROUTER_SELECT_COLOR)
                    .withInt(KeyBundle.BUNDLE_KEY, busType)
                    .navigation()
            }
        }
        dismiss()
    }

    fun initData() {

        mList.clear()
        mList.add(IntelligentTouchOptionModel(LightCustomizeColorConfig.TYPE_CUS_COLOR_CYAN,isSelect = false, name="#3DC7F5"))
        mList.add(IntelligentTouchOptionModel(LightCustomizeColorConfig.TYPE_CUS_COLOR_BLUE,isSelect = false, name="#3D4DF5"))
        mList.add(IntelligentTouchOptionModel(LightCustomizeColorConfig.TYPE_CUS_COLOR_PURPLE,isSelect = false, name=ColorUtils.defColor))
        mList.add(IntelligentTouchOptionModel(LightCustomizeColorConfig.TYPE_CUS_COLOR_RED,isSelect = false, name="#FF3333"))
        mList.add(IntelligentTouchOptionModel(LightCustomizeColorConfig.TYPE_CUS_COLOR_ORANGE,isSelect = false, name="#FF9933"))
        mList.add(IntelligentTouchOptionModel(LightCustomizeColorConfig.TYPE_CUS_COLOR_YELLOW,isSelect = false, name="#FFDD33"))
        mList.add(IntelligentTouchOptionModel(LightCustomizeColorConfig.TYPE_CUS_COLOR_GREEN,isSelect = false, name="#3DF58A"))

        selectLightColorAdapter.setList(mList)
    }

    /**
     * set dialog theme(设置对话框主题)
     */
    private fun setDialogTheme() {
        requestWindowFeature(Window.FEATURE_NO_TITLE) // android:windowNoTitle
        window?.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) // android:backgroundDimEnabled默认是true的
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) // android:windowBackground
    }

    /**
     * @param listener
     */
    fun setListener(listener: OnSelectLightColorDialogListener?) {
        this.lightColorDialogListener = listener
    }


    /**
     * 回调接口
     */
    interface OnSelectLightColorDialogListener {
        fun onSelectLightColorClick(mData: IntelligentTouchOptionModel)
    }
}
